<template>
  <div class="table-box">
    <el-table
      ref="mytableRef"
      :data="tableData"
      stripe
      max-height="59vh"
      :header-cell-style="{ color: '#fff', backgroundColor: '#4977af' }"
      :row-style="{ height: '80px' }"
      @select="handleSelectionChange"
      @select-all="handleSelectionChange"
      class="mytable"
    >
      <el-table-column
        type="index"
        :index="indexMethod"
        label="序号"
        width="60"
        align="center"
      />

      <el-table-column label="姓名" align="center">
        <template #default="scope">
          <p>
            {{ scope.row.memberName }}
          </p>
        </template>
      </el-table-column>

      <el-table-column label="性别" align="center">
        <template #default="scope">
          <p>
            {{ scope.row.sex }}
          </p>
        </template>
      </el-table-column>

      <el-table-column label="学号" align="center">
        <template #default="scope">
          <p>
            {{ scope.row.studentID }}
          </p>
        </template>
      </el-table-column>

      <el-table-column label="学院" align="center">
        <template #default="scope">
          <p>
            {{ scope.row.college }}
          </p>
        </template>
      </el-table-column>

      <el-table-column label="加入时间" align="center">
        <template #default="scope">
          <p>
            {{ scope.row.joinTime }}
          </p>
        </template>
      </el-table-column>

      <el-table-column label="联系电话" align="center">
        <template #default="scope">
          <p>
            {{ scope.row.phone }}
          </p>
        </template>
      </el-table-column>

      <el-table-column label="所在社团及职位" align="center">
        <template #default="scope">
          <p
            :title="computePosition(scope.$index)"
            v-if="store.getters.organizationMap"
          >
            {{ computePosition(scope.$index) }}
          </p>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="80" align="center">
        <template #default="scope">
          <div class="operation">
            <span
              v-if="!isMoving[scope.$index]"
              @click="isMoving[scope.$index] = !isMoving[scope.$index]"
              >移动</span
            >

            <span v-else class="move">
              <el-icon class="moveup-text" @click="handleMoveUp(scope.$index)">
                <ArrowUpBold />
              </el-icon>

              <el-icon
                class="movedown-text"
                @click="handleMoveDown(scope.$index)"
              >
                <ArrowDownBold />
              </el-icon>
            </span>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { computed, reactive, watchEffect, watch, ref, toRefs } from "vue";
import { ElTable } from "element-plus";
import lodash from "lodash";


import { selects, options } from "@/data/select.ts";
import { organizationMemberMap } from "@/data/tableMap.ts";
import { useStore } from "vuex";

interface ITable {
  [key: string]: any;
}

const props = defineProps(["tableData", "selectedTable", "pagination"]);
const emits = defineEmits(["moveUp", "moveDown", "selectionChange"]);

// 初始化结束

const store = useStore();

const hiddenStr = computed(() => {
  return function (str: string) {
    return '*'.repeat(str.length - 4) + str.slice(str.length - 4);
  }
})

const computePosition = computed(() => {
  return function (index: number) {
    // console.log(props.tableData[index].positions);

    const positions = props.tableData[index].positions;

    let result = "";
    const getters = store.getters;

    for (const item of positions) {
      result += `${getters.organizationMap[item.organization]} `;

      result += `${item.position};\n`;
    }

    result = result.slice(0, result.length - 1);

    return result;
  };
});

const getAge = computed(() => {
  return function (index: number) {
    return (
      new Date().getFullYear() -
      Number.parseInt(props.tableData[index].birthday)
    );
  };
});

const totalPartyMonth = (index: number) => {
  return (
    (new Date().getFullYear() -
      Number.parseInt(props.tableData[index].joinTime) -
      1) *
      12 +
    (new Date().getMonth() + 1) +
    (12 - props.tableData[index].joinTime.split("-")[1])
  );
};

const getPartyYear = computed(() => {
  return function (index: number) {
    return Math.floor(totalPartyMonth(index) / 12);
  };
});

const getPartyMonth = computed(() => {
  return function (index: number) {
    return (
      totalPartyMonth(index) - Math.floor(totalPartyMonth(index) / 12) * 12
    );
  };
});

const mytableRef = ref<InstanceType<typeof ElTable>>();

const handleSelectionChange = (selection: ITable[]) => {
  // console.log(selection);
  emits("selectionChange", selection);
};

watch(
  () => props.tableData,
  () => {
    const selectedTable = [];
    for (const item in props.selectedTable) {
      if (props.selectedTable[item].length != 0) {
        selectedTable.push(...props.selectedTable[item]);
      }
    }

    for (const item of props.tableData) {
      for (const selectItem of selectedTable) {
        if (selectItem.rowId === item.rowId) {
          mytableRef.value?.toggleRowSelection(item, true);
        }
      }
    }
  },
  {
    deep: true,
  }
);

type IsMoving = boolean | undefined;

const isMoving: IsMoving[] = reactive([]);
const handleMoveUp = (index: number) => {
  if (index !== 0) {
    emits("moveUp", index);
  }

  isMoving[index] = false;
};

const handleMoveDown = (index: number) => {
  if (index !== props.tableData.length - 1) {
    emits("moveDown", index);
  }

  isMoving[index] = false;
};

const indexMethod = (index: number) => {
  return index + 1;
};
</script>

<style lang="less" scoped>
.table-box {
  // width: calc(100vw - 240px);
  .mytable {
    width: 100%;
    height: 59vh;
    p {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }

    .operation {
      display: flex;
      align-items: center;
      justify-content: center;

      span {
        color: #3f85ed;
        cursor: pointer;
        margin-right: 20px;
      }

      span:last-child {
        margin-right: 0;
      }

      .moveup-text,
      .movedown-text {
        color: #666;
      }

      .delete-text {
        margin-left: 20px;
      }

      .move {
        display: inline-flex;
        flex-direction: column;
        font-size: 16px;
        margin-left: 8px;
      }
    }
  }
}

:deep(.el-dialog_body) {
  height: 40vh;
  overflow: auto;
}

p {
  white-space: pre-wrap;
}
</style>
